<template>
  <Card title="告警指标" :loading="loading">
    <div ref="chartRef" :style="{ width, height }"></div>
  </Card>
</template>
<script lang="ts" setup>
  import { onMounted, Ref, ref, watch } from 'vue';
  import { Card } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { StdResponse } from '/@/api/model/baseModel';
  import { getAlarmLogsRadar } from '/@/api/monitor/rule';
  import { GetAlarmRadarRsp } from '/@/api/monitor/entity/entity';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { RadarIndicatorOption } from 'echarts/types/src/coord/radar/RadarModel';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '300px',
    },
  });
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const { createMessage } = useMessage();

  const countData = ref<GetAlarmRadarRsp>();
  const indicatorsData = ref<RadarIndicatorOption[]>([]);

  const loadData = async () => {
    const rsp: StdResponse = await getAlarmLogsRadar({ length: 7 * 24 });
    if (!rsp) {
      createMessage.error('获取数据失败');
    }
    let data: GetAlarmRadarRsp = { metric: [], level_1: [], level_2: [], level_3: [], max: 0 };
    data = rsp;
    for (let mt of data.metric) {
      indicatorsData.value.push({ text: mt, max: data.max });
    }
    countData.value = data;
  };

  onMounted(() => {
    loadData();
  });

  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      setOptions({
        legend: {
          bottom: 0,
          data: ['1级', '2级', '3级'],
        },
        tooltip: {},
        radar: {
          radius: '60%',
          splitNumber: 8,
          indicator: indicatorsData.value,
        },
        series: [
          {
            type: 'radar',
            symbolSize: 0,
            areaStyle: {
              shadowBlur: 0,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1,
            },
            data: [
              {
                value: countData.value?.level_3,
                name: '3级',
                itemStyle: {
                  color: '#2ec7c9',
                },
              },
              {
                value: countData.value?.level_2,
                name: '2级',
                itemStyle: {
                  color: '#5ab1ef',
                },
              },
              {
                value: countData.value?.level_1,
                name: '1级',
                itemStyle: {
                  color: '#b6a2de',
                },
              },
            ],
          },
        ],
      });
    },
    { immediate: true },
  );
</script>
